﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace THenu.WinWapperShellSeed
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        private const string PATHFILE = "Person.dBlog";
        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(PATHFILE))
            {
                Person info = new Person();
                info.ReadFromFile();
                txtName.Text = info.Name;
                txtRemark.Text = info.Remark;
            }
        }
        private void btnAppend_Click(object sender, EventArgs e)
        {
            Person info = new Person();

            info.Name = txtName.Text;
            info.Remark = txtRemark.Text;

            info.SaveToFile();
        }
        /// <summary>
        /// 
        /// </summary>
        [Serializable]
        public class Person
        {

            public string Name { get; set; }

            public string Remark { get; set; }

            public Boolean SaveToFile()
            {

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
                using (XmlTextWriter xmlTWriter = new XmlTextWriter(PATHFILE, Encoding.UTF8))
                {
                    xmlSerializer.Serialize(xmlTWriter, this);
                }
                return true;
            }
            public Person ReadFromFile()
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
                using (Stream xmlTWriter = File.OpenRead(PATHFILE))
                {
                    Person pNew = (Person)xmlSerializer.Deserialize(xmlTWriter);
                    this.Name = pNew.Name;
                    this.Remark = pNew.Remark;
                    return pNew;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (File.Exists(PATHFILE))
            {
                Person info = new Person();
                info.ReadFromFile();
                txtName.Text = info.Name;
                txtRemark.Text = info.Remark;
            }
        }
    }
}
